package com.sky.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.RedisConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.mapper.admin.CategoryMapper;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.CategoryService;
import org.junit.jupiter.params.shadow.com.univocity.parsers.annotations.Copy;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品或套餐分类
     * @param categoryDTO
     * @return
     */
    @Override
    public Result<Category> insert(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        category.setStatus(StatusConstant.ENABLE);

        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.insert(category);
        return Result.success();
    }

    /**
     * 修改菜品或套餐分类
     * @param categoryDTO
     * @return
     */
    @Override
    public Result update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
        return Result.success();
    }

    /**
     * 分页查询菜品或套餐分类
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public Result<PageResult> selectPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());

        List<Category> categoryList = categoryMapper.selectPage(categoryPageQueryDTO);

        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        PageResult pb = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return Result.success(pb);
    }

    /**
     * 修改菜品或套餐分类状态
     * @param status
     * @param id
     * @return
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        Category category = new Category();
        category.setId(id);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setStatus(status);
        categoryMapper.updateStatus(category);
    }

    /**
     * 根据分类类型查询分类数据
     * @param type
     * @return
     */
    @Override
    public Result<List<Category>> selectByType( Integer type) {
        List<Category> categoryList  = (List<Category>)redisTemplate.opsForValue().get(RedisConstant.CATEGORY_LIST + type);
        if(categoryList == null || categoryList.size() == 0) {
            categoryList = categoryMapper.selectByType(1);
            List<Category> categoryList2 = categoryMapper.selectByType(2);
            categoryList.addAll(categoryList2);
            redisTemplate.opsForValue().set(RedisConstant.CATEGORY_LIST + type, categoryList);
        }
        return Result.success(categoryList);
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Long id) {
        categoryMapper.deleteById(id);
        return Result.success();
    }


}
